//
//  ACVertifyCodeViewController.m
//  ACLiteAVUISDK_Example
//
//  Created by 冯琰琰 on 2021/3/29.
//  Copyright © 2021 fyanyan. All rights reserved.
//

#import "ACVertifyCodeViewController.h"
#import <CRBoxInputView.h>
#import <Masonry.h>
#import <ACLiteAVUISDK.h>
#import "GenerateTestUserSig.h"

@interface ACVertifyCodeViewController ()
@property (weak, nonatomic) IBOutlet UILabel *phoneStr;
@property (weak, nonatomic) IBOutlet UIView *vertifyCodeView;
@property (weak, nonatomic) IBOutlet UIButton *reSendBtn;


@end

@implementation ACVertifyCodeViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
    self.fd_prefersNavigationBarHidden = YES;
    self.fd_interactivePopDisabled = YES;
    self.fd_interactivePopMaxAllowedInitialDistanceToLeftEdge = 200;
    
    CRBoxInputCellProperty *cellProperty = [CRBoxInputCellProperty new];
    cellProperty.cellBgColorNormal = UIColorFromRGB(0xF0EFEF);
    cellProperty.cellBgColorSelected = UIColorFromRGB(0xF0EFEF);
    cellProperty.cellCursorColor = UIColorFromRGB(0xF0EFEF);
    cellProperty.cellCursorWidth = 2;
    cellProperty.cellCursorHeight = 30;
    cellProperty.cornerRadius = 2;
    cellProperty.borderWidth = 0;
    cellProperty.cellFont = [UIFont systemFontOfSize:32 weight:UIFontWeightMedium];
    cellProperty.cellTextColor = UIColorFromRGB(0x303133);

    CRBoxInputView *boxInputView = [[CRBoxInputView alloc] initWithCodeLength:6];
    boxInputView.boxFlowLayout.itemSize = CGSizeMake(40, 52);
    boxInputView.customCellProperty = cellProperty;
    [boxInputView loadAndPrepareViewWithBeginEdit:YES];
    
    [self.vertifyCodeView addSubview:boxInputView];
    [boxInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    @weakify(self)
    boxInputView.textDidChangeblock = ^(NSString * _Nullable text, BOOL isFinished) {
        @strongify(self)
        if (isFinished) {
            [self loginToACLiveWithVertifyCode:text];
        }
    };
    self.phoneStr.text = [NSString stringWithFormat:@"已发送验证到 %@",_phoneNum];
}

-(void)setPhoneNum:(NSString *)phoneNum{
    _phoneNum = phoneNum;
}

-(void)loginToACLiveWithVertifyCode:(NSString *)vertifyCode{
    dispatch_queue_t loginQueue = dispatch_queue_create("LoginQueue", 0);
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    [SVProgressHUD showWithStatus:@""];
    dispatch_async(loginQueue, ^{
        [ACLoginManager requestLoginWithPhone:[self->_phoneNum stringByReplacingOccurrencesOfString:@" " withString:@""] vertifyCode: vertifyCode requestCallback:^(int code, NSString * _Nullable message) {
            if (code == 0) {
                dispatch_semaphore_signal(semaphore);
            }else{
                [SVProgressHUD dismiss];
                [SVProgressHUD showInfoWithStatus:@"登录失败"];
            }
        }];
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        [ACLoginManager requestUserSigRequestCallback:^(id  _Nonnull responseObject) {
            DDLogInfo(@"%@",responseObject);
            NSString *userSig = [responseObject objectForKey:@"sign"];
            [ACUserProfileManager shareInstance].userSig = userSig;
//            [ACUserProfileManager shareInstance].userSig = [GenerateTestUserSig genTestUserSig:[ACUserProfileManager shareInstance].userModel.user_id];
            [TRTCLiveRoomIMAction loginWithUserID:[ACUserProfileManager shareInstance].userModel.user_id userSig: userSig callback:^(int code, NSString * _Nonnull message) {
                if (code == 0) {
                    dispatch_semaphore_signal(semaphore);
                }else{
                    [SVProgressHUD dismiss];
                    [SVProgressHUD showInfoWithStatus:@"登录失败"];
                }
            }];
        }];
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        NSString *userName = [@"用户_" stringByAppendingString:[[self->_phoneNum componentsSeparatedByString:@" "] lastObject]];
        [ACLoginManager requestSetUserProfileWithNickname:userName avatar:@"https://htsec-demo-1302545507.cos.ap-nanjing.myqcloud.com/avatar.png" requestCallback:^(int code, NSString * _Nullable message) {
            [TRTCLiveRoomIMAction setProfileWithName:userName avatar:@"https://htsec-demo-1302545507.cos.ap-nanjing.myqcloud.com/avatar.png" callback:^(int code, NSString * _Nonnull message) {
                
            }];
            dispatch_semaphore_signal(semaphore);
        }];
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            ACTabbarViewController *tabVC = [[ACTabbarViewController alloc] init];
            [self.navigationController pushViewController:tabVC animated:YES];
        });
    });
}

@end
